home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / MacPNG Library 1.02 / pngMacSrc 1.02 / png.1 / pngtest.c < prev    next >
C/C++ Source or Header  |  1995-08-24  |  4KB  |  199 lines

  1. /* pngtest.c - a simple test program to test libpng 
  2.  
  3.    libpng 1.0 beta 1 - version 0.71
  4.    For conditions of distribution and use, see copyright notice in png.h
  5.    Copyright (c) 1995 Guy Eric Schalnat, Group 42, Inc.
  6.    June 26, 1995
  7.    */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "png.h"
  12.  
  13. #ifdef __TURBOC__
  14. #include <mem.h>
  15. #endif
  16.  
  17. #if defined(applec) || defined(__MWERKS__) || defined(THINK_C)        /* RMF Added */
  18. #define MACOS
  19. #include <string.h>
  20. // int memcmp(char *b1, char *b2, int size);
  21. #endif
  22.  
  23. #define OutputInfo    1    /* RMF */
  24.  
  25. /* defined so I can write to a file on gui/windowing platforms */
  26. #define STDERR stderr
  27.  
  28. /* input and output filenames */
  29. char inname[] = "pngtest.png";
  30. char outname[] = "pngout.png";
  31.  
  32. png_struct read_ptr;
  33. png_struct write_ptr;
  34. png_info info_ptr;
  35. png_info end_info;
  36.  
  37. char inbuf[256], outbuf[256];
  38.  
  39. int main()
  40. {
  41.     FILE *fpin, *fpout;
  42.     png_byte *row_buf;
  43.     png_uint_32 rowbytes;
  44.     png_uint_32 y;
  45.     int channels, num_pass, pass;
  46.  
  47.     row_buf = (png_byte *)0;
  48.  
  49.     fpin = fopen(inname, "rb");
  50.     if (!fpin)
  51.     {
  52.         fprintf(STDERR, "Could not find input file %s\n", inname);
  53.         return -1;
  54.     }
  55.  
  56.     fpout = fopen(outname, "wb");
  57.     if (!fpin)
  58.     {
  59.         fprintf(STDERR, "could not open output file %s\n", outname);
  60.         fclose(fpin);
  61.         return -1;
  62.     }
  63.  
  64.     if (setjmp(read_ptr.jmpbuf))
  65.     {
  66.         fprintf(STDERR, "libpng read error\n");
  67.         fclose(fpin);
  68.         fclose(fpout);
  69.         return -1;
  70.     }
  71.  
  72.     if (setjmp(write_ptr.jmpbuf))
  73.     {
  74.         fprintf(STDERR, "libpng write error\n");
  75.         fclose(fpin);
  76.         fclose(fpout);
  77.         return -1;
  78.     }
  79.  
  80.     png_read_init(&read_ptr);
  81.     png_write_init(&write_ptr);
  82.     png_info_init(&info_ptr);
  83.     png_info_init(&end_info);
  84.  
  85.     png_init_io(&read_ptr, fpin);
  86.     png_init_io(&write_ptr, fpout);
  87.  
  88.     png_read_info(&read_ptr, &info_ptr);
  89.     png_write_info(&write_ptr, &info_ptr);
  90.  
  91. #if OutputInfo    /* RMF */
  92.     fprintf(STDERR, "%s, Width = %d, Height = %d, Depth = %d\n",
  93.         inname, info_ptr.width, info_ptr.height, info_ptr.bit_depth);
  94.     fprintf(STDERR, "Color type = %d, interlace_type = %d\n",
  95.         info_ptr.color_type, info_ptr.interlace_type);
  96. #endif
  97.  
  98.     if ((info_ptr.color_type & 3) == 2)
  99.         channels = 3;
  100.     else
  101.         channels = 1;
  102.     if (info_ptr.color_type & 4)
  103.         channels++;
  104.  
  105.     rowbytes = ((info_ptr.width * info_ptr.bit_depth * channels + 7) >> 3);
  106.     row_buf = (png_byte *)malloc((size_t)rowbytes);
  107.     if (!row_buf)
  108.     {
  109.         fprintf(STDERR, "no memory to allocate row buffer\n");
  110.         png_read_destroy(&read_ptr, &info_ptr, (png_info *)0);
  111.         png_write_destroy(&write_ptr);
  112.         fclose(fpin);
  113.         fclose(fpout);
  114.         return -1;
  115.     }
  116.  
  117.     if (info_ptr.interlace_type)
  118.     {
  119.         num_pass = png_set_interlace_handling(&read_ptr);
  120.         num_pass = png_set_interlace_handling(&write_ptr);
  121.     }
  122.     else
  123.     {
  124.         num_pass = 1;
  125.     }
  126.  
  127.     for (pass = 0; pass < num_pass; pass++)
  128.     {
  129.         for (y = 0; y < info_ptr.height; y++)
  130.         {
  131.             png_read_rows(&read_ptr, &row_buf, (png_byte **)0, 1);
  132.             png_write_rows(&write_ptr, &row_buf, 1);
  133.         }
  134.     }
  135.  
  136.     png_read_end(&read_ptr, &end_info);
  137.     png_write_end(&write_ptr, &end_info);
  138.  
  139.     png_read_destroy(&read_ptr, &info_ptr, &end_info);
  140.     png_write_destroy(&write_ptr);
  141.  
  142.     fclose(fpin);
  143.     fclose(fpout);
  144.  
  145.     free(row_buf);
  146.  
  147. /** check if input file is the same as output file **/
  148.  
  149.     fpin = fopen(inname, "rb");
  150.  
  151.     if (!fpin)
  152.     {
  153.         fprintf(STDERR, "could not find file %s\n", inname);
  154.         return -1;
  155.     }
  156.  
  157.     fpout = fopen(outname, "rb");
  158.     if (!fpout)
  159.     {
  160.         fprintf(STDERR, "could not find file %s\n", outname);
  161.         fclose(fpin);
  162.         return -1;
  163.     }
  164.  
  165.     while (1)
  166.     {
  167.         int num_in, num_out;
  168.  
  169.         num_in = fread(inbuf, 1, 256, fpin);
  170.         num_out = fread(outbuf, 1, 256, fpout);
  171.  
  172.         if (num_in != num_out)
  173.         {
  174.             fprintf(STDERR, "files are of a different size\n");
  175.             fclose(fpin);
  176.             fclose(fpout);
  177.             return -1;
  178.         }
  179.  
  180.         if (!num_in)
  181.             break;
  182.  
  183.         if (memcmp(inbuf, outbuf, num_in))
  184.         {
  185.             fprintf(STDERR, "files are different\n");
  186.             fclose(fpin);
  187.             fclose(fpout);
  188.             return -1;
  189.         }
  190.     }
  191.  
  192.     fclose(fpin);
  193.     fclose(fpout);
  194.     
  195.     fprintf(STDERR, "libpng passes test\n");
  196.     
  197.    return 0;
  198. }
  199.